/**
*
* AndroidAlert.as
* Niels Overwijn
* 
* Copyright (c) 2010 Niels Overwijn
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package niels.android
{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	
	/**
	 * Adds a small Android-like alert box to the stage.
	 * <p>
	 * 	<strong>Usage:</strong>
	 * 	<code>
	 * 		AndroidAlert.alert(this, "Message to be shown in alert");
	 * 	</code>
	 * </p>
	 */
	public class AndroidAlert
	{
		
		public static var alerts:Array = new Array();
		public static var timer:Timer = new Timer(AndroidConfig.ALERT_DISPLAY_TIME);
		
		//////////////////////
		// PUBLIC FUNCTIONS //
		//////////////////////
		
		/**
		 * Adds the alert box to the stage
		 * @param	stage This is the stage where the alert box should be added
		 * @param	message This is te message that should be shown in the alert box
		 */
		public static function alert(stage:DisplayObjectContainer, message:String):void
		{
			var newAlert:Sprite = AndroidAlert.getAlertAsset(message);
			newAlert.visible = false;
			stage.addChild(newAlert);
			
			AndroidAlert.alerts.push(newAlert);
			if (!AndroidAlert.timer.running)
			{
				newAlert.visible = true;
				AndroidAlert.timer.addEventListener(TimerEvent.TIMER, AndroidAlert.removeAlert);
				AndroidAlert.timer.start();
			}
		}
		
		/**
		 * Used to remove the alertbox and check if any other alert is in que.
		 * Is called by the timer on the alert
		 * @param	e
		 */
		public static function removeAlert(e:TimerEvent):void
		{
			var oldAlert:Sprite = AndroidAlert.alerts.shift();
			oldAlert.parent.removeChild(oldAlert);
			AndroidAlert.timer.stop();
			AndroidAlert.timer.removeEventListener(TimerEvent.TIMER, AndroidAlert.removeAlert);
			
			if (AndroidAlert.alerts.length > 0)
			{
				AndroidAlert.alerts[0].visible = true;
				AndroidAlert.timer.addEventListener(TimerEvent.TIMER, AndroidAlert.removeAlert);
				AndroidAlert.timer.start();
			}
		}
		
		/**
		 * Returns the sprite with the alert
		 * @param message Message that is shown in the label
		 * @return alert Sprite
		 */
		public static function getAlertAsset(message:String):Sprite
		{
			var txt:TextField = new TextField();
			txt.text = message;
			txt.y = 5;
			txt.autoSize = TextFieldAutoSize.CENTER;
			txt.setTextFormat(AndroidConfig.ALERT_TEXT_FORMAT);
			txt.selectable = false;
			
			var alertAsset:Sprite = new Sprite();
			alertAsset.graphics.clear();
			alertAsset.graphics.lineStyle(2, AndroidConfig.ALERT_BORDER_COLOR, AndroidConfig.ALERT_ALPHA);
			alertAsset.graphics.beginFill(AndroidConfig.ALERT_BACKGROUND_COLOR, AndroidConfig.ALERT_ALPHA);
			alertAsset.graphics.drawRoundRect(0, 0, txt.width+40, txt.height+10, 5, 5);
			alertAsset.graphics.endFill();
			
			
			alertAsset.x = (AndroidConfig.WIDTH / 2) - (alertAsset.width / 2);
			alertAsset.y = (AndroidConfig.HEIGHT - ((AndroidConfig.HEIGHT / 100) * AndroidConfig.ALERT_Y_PERC_FROM_BOTTOM)) - (alertAsset.height / 2);
			
			alertAsset.addChild(txt);
			
			txt.x = 20;
			
			if (AndroidConfig.ALERT_DROP_SHADOW_ENABLED)
			{
				alertAsset.filters = [AndroidConfig.ALERT_DROP_SHADOW_FILTER]
			}
			
			return alertAsset;
		}
		
	}

}